var t = getApp(), a = void 0;

Page({
    data: {
        cityName: "杭州市",
        query: {
            keyword: "",
            pageSize: 15,
            pageIndex: 1
        },
        home: {},
        company: {},
        addressList: [],
        recent: []
    },
    chooseCity: function() {
        wx.navigateTo({
            url: "/pages/address/city/city"
        });
    },
    onLoad: function(t) {
        (a = this).initLoad(t), a.getRecentAddress();
    },
    onShow: function() {
        a.getHomeAndCompany();
    },
    initLoad: function(t) {
        a.setData({
            type: t.type,
            text: t.text,
            page: t.page ? t.page : "",
            cityName: wx.getStorageSync("city_name")
        }), wx.setNavigationBarTitle({
            title: t.text + "地址"
        });
    },
    getRecentAddress: function() {
        t.com.post("wx/address/recent", {}, function(e) {
            if (0 === e.errno) {
                for (var s in e.data) e.data[s].recent_use && (e.data[s].recent_use = t.com.formatMsgTime(new Date(e.data[s].recent_use).valueOf())), 
                e.data[s].create_time = t.com.formatMsgTime(new Date(e.data[s].create_time).valueOf());
                a.setData({
                    recent: e.data
                });
            }
        });
    },
    searchAddress: function(t) {
        this.setData({
            "query.keyword": t.detail.value
        }), "" != t.detail.value && this.getAddress(0);
    },
    editHome: function() {
        wx.navigateTo({
            url: "/pages/address/supply/supply?type=" + this.data.type + "&text=家的&address_default=home_address_id&address=" + JSON.stringify(this.data.home)
        });
    },
    editCompany: function() {
        wx.navigateTo({
            url: "/pages/address/supply/supply?type=" + this.data.type + "&text=公司的&address_default=company_address_id&address=" + JSON.stringify(this.data.company)
        });
    },
    editAddress: function(t) {
        t.currentTarget.dataset.index;
        wx.navigateTo({
            url: "/pages/address/supply/supply?type=" + this.data.type + "&text=" + this.data.text + "&page=address&address=" + JSON.stringify(this.data.company)
        });
    },
    getAddress: function(e) {
        1 == e ? this.data.query.pageIndex += 1 : this.data.query.pageIndex = 1, t.com.post("wx/location/search", {
            keyword: this.data.query.keyword,
            city_name: this.data.cityName,
            pageSize: this.data.query.pageSize,
            pageIndex: this.data.query.pageIndex
        }, function(t) {
            if (0 === t.errno) {
                var s = [];
                if (1 == e) {
                    s = a.data.addressList;
                    for (var d in t.data) s.push(t.data[d]);
                } else s = t.data;
                a.setData({
                    addressList: t.data
                });
            }
        });
    },
    onReachBottom: function() {
        this.data.query.keyword && this.getAddress(1);
    },
    chooseAddress: function(t) {
        var a = t.currentTarget.dataset.index, e = getCurrentPages(), s = getCurrentPages()[e.length - 2];
        "supply" == this.data.page ? s.setData({
            address: this.data.recent[a]
        }) : "start" == this.data.type ? s.setData({
            startAddress: this.data.recent[a]
        }) : "end" == this.data.type && s.setData({
            endAddress: this.data.recent[a]
        }), wx.navigateBack({
            delta: 1
        });
    },
    takeThisAddress: function(t) {
        var a = t.currentTarget.dataset.index, e = getCurrentPages(), s = getCurrentPages()[e.length - 2];
        "supply" == this.data.page && s.setData({
            "address.id": "",
            "address.formatted_addresse": this.data.addressList[a].title,
            "address.province": this.data.addressList[a].ad_info.province,
            "address.city": this.data.addressList[a].ad_info.city,
            "address.district": this.data.addressList[a].ad_info.district,
            "address.latitude": this.data.addressList[a].location.lat,
            "address.longitude": this.data.addressList[a].location.lng,
            "address.street_number": this.data.addressList[a].address.replace(this.data.addressList[a].ad_info.province, "").replace(this.data.addressList[a].ad_info.city, "").replace(this.data.addressList[a].ad_info.district, "")
        }), wx.navigateBack({
            delta: 1
        });
    },
    getHomeAndCompany: function() {
        t.com.post("wx/address/hc", {}, function(t) {
            0 === t.errno && a.setData(t.data);
        });
    },
    chooseHomeAddress: function() {
        if (this.data.home.city) {
            var t = getCurrentPages(), a = getCurrentPages()[t.length - 2];
            "supply" == this.data.page ? a.setData({
                address: this.data.home
            }) : "start" == this.data.type ? a.setData({
                startAddress: this.data.home
            }) : "end" == this.data.type && a.setData({
                endAddress: this.data.home
            }), wx.navigateBack({
                delta: 1
            });
        }
    },
    chooseCompanyAddress: function() {
        if (this.data.company.city) {
            var t = getCurrentPages(), a = getCurrentPages()[t.length - 2];
            "supply" == this.data.page ? a.setData({
                address: this.data.company
            }) : "start" == this.data.type ? a.setData({
                startAddress: this.data.company
            }) : "end" == this.data.type && a.setData({
                endAddress: this.data.company
            }), wx.navigateBack({
                delta: 1
            });
        }
    }
});